package com.hbmcc.heshenghuo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;


import com.zhihaoliang.busEncy.R;

import java.util.List;

/**
 * Created by Veyron on 2017/2/20.
 * Function：自定义ViewFlipper控件
 */

public class UpView extends ViewFlipper {


    public UpView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        //设置时间间隔2000毫秒
        setFlipInterval(5000);
        //进来的动画
        Animation animIn = AnimationUtils.loadAnimation(context, R.anim.anim_in);
        setInAnimation(animIn);
        //退出的动画
        Animation animOut = AnimationUtils.loadAnimation(context, R.anim.anim_out);
        setOutAnimation(animOut);
    }

    /**
     * 设置循环滚动的View数组
     */
    public void setViews(final List<View> views) {
        if (views == null || views.isEmpty()) {
            return;
        }
        removeAllViews();
        for (int i = 0; i < views.size(); i++) {
            if (views.get(i).getParent() != null) {
                ((ViewGroup)views.get(i).getParent()).removeView(views.get(i));
            }
            addView(views.get(i));
        }
        if(views.size()>1){
            //开启翻滚
            startFlipping();
        }
    }
}
